<?php
# Traffic Control Functions
class Helper_Traffic
{
    private $t;
    
    /**
     * Broswer Detect
     * 存在时，返回 browser['onList'=>TRUE,  'identifier'=>$browser]
     * 不存在，返回 browser['onList'=>FALSE, 'identifier'=>$_SERVER['HTTP_USER_AGENT']]
     *
     * --%SAMPLE%--
     * $browser = array(
     *     'onList'     => TRUE,
     *     'identifier' => 'MSIE',
     * );
     *
     * @{return} array
     */
    public function __construct()
    {
        /**
         *
         * $this->t 为初始化设置
         *
         */       
		$this->t = array(
			# browser_detect
			'onList'     => true,
			'identifier' => $_SERVER['HTTP_USER_AGENT'],
			
			# ip_detect
			'ip'         => $_SERVER['REMOTE_ADDR'],
			
			# uri_detect
			'uri'        => strtolower($_SERVER['REQUEST_URI']),
			
			# referer_detect
			'referer'    => 'DIRECT',
			
			# _TIME for hits counter
			'now'        => $_SERVER['REQUEST_TIME'],
			
			# week_detect
			'lastDayOfWeek'  => date('Y/m/d', strtotime('Sunday')),
			'firstDayOfWeek' => date('Y/m/d', strtotime('Monday')-604800),
		);
    }
    
    public function getMyTraffic()
    {
        $this->_t_pkg_browserDetect();
        $this->_t_pkg_ipDetect();
        $this->_t_pkg_uriDetect();
        $this->_t_pkg_refererDetect();
        return $this->t;
    }

    private function _t_pkg_browserDetect() {

        $userAgent = $_SERVER['HTTP_USER_AGENT'];

        // onList : Tencent Traveler, Google Chrome, Konquerer, Shiretoko,
        //          Opera, Safari, Firefox , MSIE

        do
        {
            if(strpos($userAgent, 'Tencent Traveler') !== FALSE)
            {
                $this->t['identifier'] = 'Tencent Traveler';
                break;
            }
            elseif(strpos($userAgent, 'Chrome') !== FALSE)
            {
                $this->t['identifier'] = 'Google Chrome';
                break;
            }
            elseif(strpos($userAgent, 'Konquerer') !== FALSE)
            {
                $this->t['identifier'] = 'Konquerer';
                break;
            }
            elseif(strpos($userAgent, 'Shiretoko') !== FALSE)
            {
                $this->t['identifier'] = 'Shiretoko';
                break;
            }
            elseif(strpos($userAgent, 'Opera') !== FALSE)
            {
                $this->t['identifier'] = 'Opera';
                break;
            }
            // Firefox 与 MSIE
            elseif(strpos($userAgent, 'Firefox') !== FALSE)
            {
                $this->t['identifier'] = 'Firefox';
                break;
            }
            elseif(strpos($userAgent, 'Safari') !== FALSE && strpos($userAgent, 'Chrome') === FALSE)
            {
                $this->t['identifier'] = 'Safari';
                break;
            }


            $isIE = strpos($userAgent, 'MSIE') !== FALSE ? TRUE : FALSE;
            if($isIE && strpos($userAgent, 'MSIE 7.0') !== FALSE)
            {
                $this->t['identifier'] = 'Microsoft IE 7.0';
                break;
            }
            elseif($isIE && strpos($userAgent, 'MSIE 8.0') !== FALSE)
            {
                $this->t['identifier'] = 'Microsoft IE 8.0';
                break;
            }
            elseif($isIE)
            {
                $this->t['identifier'] = 'Microsoft IE 6.0'; // 也有可能版本更低
                break;
            }

            /**
             * 如果所有 条件都不符合，输出该 HTTP_USER_AGENT
             * 以便手动判断
             */
            $this->t['onList'] = FALSE;

        } while(false);
    }

    /**
     * Ip Detect
     *
     * --%SAMPLE%--
     * $ip = array(
     *     'ip'   => '202.121.96.27',
     * );
     *
     * @{return} array
     */
    private function _t_pkg_ipDetect()
    {
    }

    /**
     * Uri Detect
     *
     * --%SAMPLE%--
     * $uri = array(
     *     'uri' => 'home/index',
     * );
     *
     * @{return} array
     */
    private function _t_pkg_uriDetect()
    {
        /*
        
        $requestUri = strtolower($_SERVER['REQUEST_URI']);

        do
        {
            if($requestUri == '/')
            {
                $uri['uri'] = '/home/index';
                break;
            }
            elseif(strpos($requestUri, 'home') !== false)
            {
                $uri['uri'] = '/home/index';
                break;
            }
            elseif(strpos($requestUri, 'newsnotice') !== false)
            {
                $uri['uri'] = '/newsnotice/index';
                break;
            }
            elseif(strpos($requestUri, 'institute') !== false)
            {
                $uri['uri'] = '/institute/index';
                break;
            }
            elseif(strpos($requestUri, 'faculty') !== false)
            {
                $uri['uri'] = '/faculty/index';
                break;
            }
            elseif(strpos($requestUri, 'education') !== false)
            {
                $uri['uri'] = '/education/index';
                break;
            }
            elseif(strpos($requestUri, 'research') !== false)
            {
                $uri['uri'] = '/research/index';
                break;
            }
            elseif(strpos($requestUri, 'file') !== false)
            {
                $uri['uri'] = '/file/index';
                break;
            }
            elseif(strpos($requestUri, 'alumni') !== false)
            {
                $uri['uri'] = '/alumni/index';
                break;
            }
        } while(false);
        
        */
    }

    /**
     * Referer Detect
     *
     * --%SAMPLE%--
     * $referer = array(
     *     'referer' => 'http://www.google.com/',
     * );
     *
     * @{return} array
     */
    private function _t_pkg_refererDetect()
    {
        if(isset($_SERVER['HTTP_REFERER']))
        {
            /*
            
            preg_match('/http:\/\/.*\.(com|net|cn|org|edu)/i', $_SERVER['HTTP_REFERER'], $match);

            if(empty($match))
            {
                preg_match('/\d{0,3}\.\d{0,3}\.\d{0,3}\.\d{0,3}/i' ,$_SERVER['HTTP_REFERER'], $match);
            }

            if(!empty($match))
            {
                $referer['referer'] = $match[0];
            }
            
            */

            if($this->t['identifier'] == ('Microsoft IE 6.0'))
            {
                $this->t['referer'] = urlencode($_SERVER['HTTP_REFERER']);
            }
            else
            {
                $this->t['referer'] = urldecode($_SERVER['HTTP_REFERER']);
            }
        }
    }
}